#include<bits/stdc++.h>
using namespace std;
#define file(x)freopen(x  ".in","r",stdin);freopen(x  ".out","w",stdout);
char buff[1<<22],*p1=buff,*p2=buff+(1<<22)-1;
template<typename T>void qread(T&x){
    int f=0;x=0;char c=getchar();
    while(!isdigit(c)){if(c=='-')f^=1;c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
}//i had to not use cim
int n,m,q,p,a[1000005],c[1000005],aa[1000005];char d[20][333333];set<int>as;
void sol(int x){
if(x==q){
    vector<int>v,tmp;for(int i=0;i<q;i++)v.push_back(i);
    for(int i=1;i<=p;i++){
        tmp.clear();
        for(int j=0;j*2<v.size();j++){int lz=(d[i][j]=='1'?v[j*2+1]:v[j*2]);
        if(a[lz]>=i)tmp.push_back(lz);else tmp.push_back(v[j*2]+v[j*2+1]-lz);}
        swap(tmp,v);
    }
    as.insert(v[0]);
  //  cout<<v[0]<<';';
    return;
}
for(int i=0;i<=p+1;i++){a[x]=i;sol(x+1);}
}
int  main(){
    file("arena");
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>aa[i-1];
    for(int i=1;i<=m;i++)cin>>c[i];
    q=1,p=0;while(q<n)q<<=1,p++;
    for(int i=1;i<=p;i++)for(int j=0;j<q;j+=1<<i)cin>>d[i][j>>i];//,cerr<<"#";;
    int t;cin>>t;while(t--){
        int aaa,bbb,ccc,ddd;cin>>aaa>>bbb>>ccc>>ddd;
        for(int i=0;i<n;i++)aa[i]^=(i&1?(i%4==1?ccc:aaa):(i&3?ddd:bbb));int res=0;
        for(int i=1;i<=m;i++){for(int j=0;j<=n+10;j++)a[j]=aa[j];
            q=1,p=0;while(q<c[i])q<<=1,p++;
        if(n<=10){as.clear();sol(c[i]);int cnt=0;
        for(auto i:as)cnt+=1+i;res^=cnt*i;//cout<<'!';
        }
        else res^=i;}cout<<res<<endl;
        for(int i=0;i<n;i++)aa[i]^=(i&1?(i%4==1?ccc:aaa):(i&3?ddd:bbb));
    }
}